<template>
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>

    <el-form-item label="横幅描述" prop="description">
      <el-input
          v-model="queryParams.description"
          clearable
          placeholder="请输入横幅描述"
          @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

  <div class="inner-container">
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="showAddDialog">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain @click="showEditDialog" :disabled="selectedIds.length!==1">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain @click="handleDelete(null)" :disabled="!selectedIds.length">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"/>
      <el-table-column prop="description" label="横幅描述"/>
      <el-table-column label="横幅描述" width="400">
        <template #default="scope">
          <el-text line-clamp="2">
            {{scope.row.description}}
          </el-text>
        </template>
      </el-table-column>

      <el-table-column label="横幅图片">
        <template #default="scope">
          <el-button type="primary" link icon="View" @click="showImg(scope.row.image)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="横幅排序" />
      <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip/>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip/>
      <el-table-column prop="updateBy" label="修改人" />
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button type="primary" link icon="Edit" @click="showEditDialog(scope.row)">修改</el-button>
          <el-button type="primary" link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" :rules="formRules" label-width="100px">

        <el-form-item label="横幅描述" prop="description">
          <el-input v-model="formData.description" placeholder="请输入横幅描述" :rows="6" type="textarea"/>
        </el-form-item>
        <el-form-item label="横幅图片" prop="image" >
          <el-input v-model="formData.image" placeholder="请输入图片" />
        </el-form-item>
        <el-form-item label="横幅排序" prop="sort">
          <el-input v-model="formData.sort" placeholder="请输入横幅排序"/>
        </el-form-item>

      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdate">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
    <el-dialog title="查看图片" v-model="imagDialogVisible" width="800" >
      <img alt="" :src="imgUrl" style="width: 700px; margin: 0 auto; display: block"/>

    </el-dialog>
  </div>

</template>

<script setup>
import {computed, onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import ElIconPicker from '@/components/ElIconPicker'
import request from '@/utils/request'

// --------------------------------------------------

const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 10})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])

const listData = () => {
  loading.value = true
  request.get('/api/bus/banner', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total
    tableData.value = res.data.list
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

const handleDelete = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的记录')
    return
  }
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.delete(`/api/bus/banner?ids=${ids}`)
        .then(res => {
          selectedIds.value.length = 0
          listData()
          ElMessage.success('删除成功')
        })
  }).catch(() => {})
}
// --------------------------------------------------
const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const imagDialogVisible = ref(false)
const imgUrl= ref('')
const formData = ref({
  id: undefined,
  description: undefined,
  image: undefined,
  sort: undefined
})
const showImg = (pic)=>{
  imgUrl.value=pic;
  imagDialogVisible.value=true;
}
const formRules = ref({
  description: [{required: true, message: '横幅描述为必填项'}],
  image: [{required: true, message: '横幅图片为必填项'}],
  sort: [{required: true, message: '横幅排序为必填项'}]
})
const showAddDialog = () => {

  dialogTitle.value = '新增横幅'
  dialogVisible.value = true
}
const showEditDialog = (row) => {
  let id = row.id || selectedIds.value
  request.get(`/api/bus/banner/${id}`)
      .then(res => {
        formData.value = res.data
        dialogTitle.value = '修改横幅'
        dialogVisible.value = true
      })
}
const closeDialog = () => {
  formData.value = {
    id: undefined,
    description: undefined,
    image: undefined,
    sort: undefined
  }
  dialogFormRef.value.resetFields()
  dialogVisible.value = false
}

const saveOrUpdate = () => {
  dialogFormRef.value.validate(valid => {
    if (!valid) {
      ElMessage.error('请正确填写参数')
      return
    }
    const id = formData.value.id
    request({
      url: '/api/bus/banner',
      method: id ? 'put' : 'post',
      data: formData.value
    }).then(res => {
      closeDialog()
      listData()

      ElMessage.success(id ? '修改成功' : '新增成功')
    });
  })

  // --------------------------------------------------

}

onMounted(() => {
  listData()
})

</script>

<style scoped>

</style>
